package com.gardenguell.salad.ui.activity;

import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentStatePagerAdapter;
import android.support.v4.view.ViewPager;
import android.widget.ImageView;
import android.widget.LinearLayout;
import com.gardenguell.salad.R;
import com.gardenguell.salad.ui.activity.base.BaseActivity;
import com.gardenguell.salad.ui.fragment.BaseFragment;
import com.gardenguell.salad.ui.fragment.WelcomeFragment;

/**
 * Created by absurd on 2015/11/14.
 */
public class WelcomeActivity extends BaseActivity implements ViewPager.OnPageChangeListener{
    private static final int NUM_PAGES = 3;

    private BaseFragment[] mFragments ;

    private ViewPager mViewPager;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_welcome);

        mFragments = new BaseFragment[NUM_PAGES];
        mViewPager = (ViewPager) findViewById(R.id.circle_viewflow);
        ScreenSlidePagerAdapter pageAdapter = new ScreenSlidePagerAdapter(getSupportFragmentManager());
        mViewPager.setAdapter(pageAdapter);
        mViewPager.setOnPageChangeListener(this);
    }

    @Override
    public void onPageScrolled(int i, float v, int i1) {

    }

    @Override
    public void onPageSelected(int position) {
        LinearLayout ll = (LinearLayout) findViewById(R.id.viewflowindicator);
        for (int i = 0; i < ll.getChildCount(); ++i) {
            ImageView iv = (ImageView) ll.getChildAt(i);
            iv.setImageResource(i == position ? R.mipmap.dot_current
                    : R.mipmap.dot_normal);
        }
    }

    @Override
    public void onPageScrollStateChanged(int i) {

    }



    public class ScreenSlidePagerAdapter extends FragmentStatePagerAdapter {
        public ScreenSlidePagerAdapter(FragmentManager fm) {
            super(fm);

            for (int i = 0; i < 3; i++) {
                Bundle args = new Bundle();
                args.putInt("position", i);
                WelcomeFragment fragment = new WelcomeFragment();
                fragment.setArguments(args);
                mFragments[i] = fragment;
            }
        }

        @Override
        public Fragment getItem(int position) {
            return mFragments[position];
        }

        @Override
        public int getCount() {
            return NUM_PAGES;
        }
    }
}